﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;

namespace Volume_Resource_Management
{
    public partial class frmMain : Form
    {
        List<volumesPerDay> volumes = new List<volumesPerDay>();
        DateTime currentDate;
        string storeNumber;

        //button information
        Button[] hfb = new Button[23];
        bool allowMove = false;

        //picturebox variables
        private int SQSize = 5;
        private int rows = 590;
        private int cols = 1255;
        private int[,] paintSQ = new int[251, 118];
        private int gridlines = 0;
        private int draw = 1;
        
        int currentYear;

        public frmMain(List<volumesPerDay> volume, int[,] storeMap, string storeNo, int year)
        {
            InitializeComponent();
            volumes = volume;
            paintSQ = storeMap;
            storeNumber = storeNo;
            currentYear = year;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = "Volume Resource Management - Store " + storeNumber;

            currentDate = DateTime.Now;

            for (int i = 0; i < volumes.Count; i++)
            {
                if(volumes[i].SS[1] != 0)
                    lstDates.Items.Add(volumes[i].date);
            }

            loadButtons();
            pictureBox1.SendToBack();
            pictureBox1.Refresh();
            if (lstDates.Items.Count != 0)
            {
                lstDates.SelectedIndex = 0;
                calcWeekInformation();
            }
        }

        public void calcWeekInformation()
        {
            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
            Calendar cal = dfi.Calendar;
            DateTime time = Convert.ToDateTime(lstDates.Text);

            int weekNo = cal.GetWeekOfYear(time, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
            int tertial = 0;

            if ((weekNo == 35 && DateTime.Now.Month == 9) || ((weekNo >= 18) && (weekNo<=34)))
            {
                tertial = 3;
            }
            else if((weekNo >=1) && (weekNo <=17))
            {
                tertial = 2;
            }
            else if ((weekNo >= 35) && (weekNo <=52))
            {
                tertial = 1;
            }
            

            toolStripLabel1.Text = "Week " + weekNo.ToString();

            List<string> hoursInfo = databaseAccessability.getHoursAndLimit(Convert.ToInt32(storeNumber), weekNo, tertial, currentYear);
            
            string totalHours = Math.Floor(Convert.ToDouble(hoursInfo[0])
                * (Convert.ToDouble(databaseAccessability.getScenario(Convert.ToInt32(storeNumber), currentYear)) / 100)).ToString();

            toolStripLabel2.Text = "Total Hours To Use This Week: " + totalHours;

            if (hoursInfo[1] == "1")
            {
                toolStripLabel3.Text = "Holiday Limit";
            }
            else
            {
                toolStripLabel3.Text = "";
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics grid = e.Graphics;

            if (gridlines == 1)
            {
                Pen myPen = new Pen(Color.LightGray, 1);

                for (int i = 0; i <= this.cols; i += this.SQSize)
                {
                    grid.DrawLine(myPen, i, 0, i, rows);
                }

                for (int i = 0; i <= this.rows; i += this.SQSize)
                {
                    grid.DrawLine(myPen, 0, i, cols, i);
                }
                myPen.Dispose();
            }

            SolidBrush myBrush = new SolidBrush(Color.Black);

            int maxRows = rows / SQSize;
            int maxCols = cols / SQSize;
            int cellRow = 0;

            while (cellRow < maxRows)
            {
                int cellCol = 0;
                while (cellCol < maxCols)
                {
                    if (paintSQ[cellCol, cellRow] == 1)
                    {
                        grid.FillRectangle(myBrush, (cellCol * this.SQSize) + 1, (cellRow * this.SQSize) + 1, this.SQSize, this.SQSize);
                    }
                    cellCol++;
                }
                cellRow++;
            }
            myBrush.Dispose();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (gridlines == 1)
            {
                try
                {
                    if (e.X < pictureBox1.Width && e.Y < pictureBox1.Height && e.X > 0 && e.Y > 0 && e.Button == MouseButtons.Left)
                    {
                        int xMouse = 0;
                        int yMouse = 0;
                        double xMouse1 = (e.X - 1) / SQSize;
                        double yMouse1 = (e.Y - 1) / SQSize;

                        xMouse = (int)System.Math.Ceiling(xMouse1);
                        yMouse = (int)System.Math.Ceiling(yMouse1);

                        this.paintSQ[xMouse, yMouse] = draw;

                        pictureBox1.Refresh();
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (gridlines == 1)
            {
                try
                {
                    if (e.X < pictureBox1.Width && e.Y < pictureBox1.Height && e.X > 0 && e.Y > 0 && e.Button == MouseButtons.Left)
                    {
                        int xMouse = 0;
                        int yMouse = 0;
                        double xMouse1 = (e.X - 1) / SQSize;
                        double yMouse1 = (e.Y - 1) / SQSize;

                        xMouse = (int)System.Math.Ceiling(xMouse1);
                        yMouse = (int)System.Math.Ceiling(yMouse1);

                        this.paintSQ[xMouse, yMouse] = draw;

                        pictureBox1.Refresh();
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }

        private void loadButtons()
        {
            for (int i = 0; i < 22; i++)
            {
                List<int> pos = databaseAccessability.getButtonLocation(Convert.ToInt32(storeNumber), i);

                hfb[i] = new Button();

                hfb[i].Location = new Point(pos[0], pos[1]);
                hfb[i].Height = 50;
                hfb[i].Width = 80;
                hfb[i].Click += HFB_Button_Click;
                hfb[i].MouseMove += HFB_Button_Move;
                hfb[i].Name = "HFB" + i;
                hfb[i].Font = new Font("Calibri", 8);
                if (i != 4)
                    this.Controls.Add(hfb[i]);
            }
        }

        private void HFB_Button_Move(object sender, MouseEventArgs e)
        {
            if(allowMove)
            {
                if(e.Button == MouseButtons.Left)
                {
                    string hfbText = ((Button)sender).Name.ToString();
                    if (hfbText.Length == 4)
                        hfbText = hfbText.Substring(hfbText.Length - 1, 1);
                    else
                        hfbText = hfbText.Substring(hfbText.Length - 2, 2);

                    moveButton(Convert.ToInt32(hfbText));
                }
            }
        }

        private void moveButton(int butNo)
        {
            hfb[butNo].Location = new Point(Cursor.Position.X - this.Left - (hfb[butNo].Width / 2), Cursor.Position.Y - this.Top - hfb[butNo].Height);
        }

        private void HFB_Button_Click(object sender, EventArgs e)
        {
            DateTime nD = DateTime.Now;
            DateTime newD = nD.AddDays(lstDates.SelectedIndex);
            string d = newD.ToString("dd-MMM-yyyy");

            if (!allowMove)
            {
                Button b = (Button)sender;
                string index;
                volumesPerDay today = new volumesPerDay();

                for (int i = 0; i < volumes.Count; i++)
                {
                    if (volumes[i].date == d)
                    {
                        today = volumes[i];
                        break;
                    }
                }

                if (b.Name.ToString().Length == 5)
                {
                    index = b.Name.ToString().Substring(b.Name.ToString().Length - 2, 2);
                    int temp = Convert.ToInt32(index);
                    temp++;
                    index = temp.ToString();
                }
                else
                {
                    index = b.Name.ToString().Substring(b.Name.ToString().Length - 1, 1);
                    int temp = Convert.ToInt32(index);
                    temp++;
                    index = "0" + temp.ToString();
                }

                if (Convert.ToInt32(index) <= 19)
                {
                    frmDailyDeliveriesPerHFB del = new frmDailyDeliveriesPerHFB(d, index, "0", storeNumber, today);
                    del.Show();
                }
                else if(Convert.ToInt32(index) == 20)
                {
                    frmDailyDeliveriesPerHFB del = new frmDailyDeliveriesPerHFB(d, "92", "0", storeNumber, today);
                    del.Show();
                }
                else if (Convert.ToInt32(index) == 21)
                {
                    frmDailyDeliveriesPerHFB del = new frmDailyDeliveriesPerHFB(d, "1", storeNumber, today);
                    del.Show();
                }
                else if (Convert.ToInt32(index) == 22)
                {
                    frmDailyDeliveriesPerHFB del = new frmDailyDeliveriesPerHFB(d, "2", storeNumber, today);
                    del.Show();
                }
            }
        }

        private void updateMap(int dayJump)
        {
            DateTime nD = DateTime.Now;
            DateTime newD = nD.AddDays(dayJump);
            string d = newD.ToString("dd-MMM-yyyy");

            for (int i = 0; i < volumes.Count(); i++)
            {
                if (d == volumes[i].date)
                {
                    hfb[0].Text = "HFB 1: " + Environment.NewLine + volumes[i].HFB1[5] + " m³";
                    hfb[1].Text = "HFB 2: " + Environment.NewLine + volumes[i].HFB2[5] + " m³";
                    hfb[2].Text = "HFB 3: " + Environment.NewLine + volumes[i].HFB3[5] + " m³";
                    hfb[3].Text = "HFB 4: " + Environment.NewLine + volumes[i].HFB4[5] + " m³";
                    hfb[5].Text = "HFB 6: " + Environment.NewLine + volumes[i].HFB6[5] + " m³";
                    hfb[6].Text = "HFB 7: " + Environment.NewLine + volumes[i].HFB7[5] + " m³";
                    hfb[7].Text = "HFB 8: " + Environment.NewLine + volumes[i].HFB8[5] + " m³";
                    hfb[8].Text = "HFB 9: " + Environment.NewLine + volumes[i].HFB9[5] + " m³";
                    hfb[9].Text = "HFB 10: " + Environment.NewLine + volumes[i].HFB10[5] + " m³";
                    hfb[10].Text = "HFB 11: " + Environment.NewLine + volumes[i].HFB11[5] + " m³";
                    hfb[11].Text = "HFB 12: " + Environment.NewLine + volumes[i].HFB12[5] + " m³";
                    hfb[12].Text = "HFB 13: " + Environment.NewLine + volumes[i].HFB13[5] + " m³";
                    hfb[13].Text = "HFB 14: " + Environment.NewLine + volumes[i].HFB14[5] + " m³";
                    hfb[14].Text = "HFB 15: " + Environment.NewLine + volumes[i].HFB15[5] + " m³";
                    hfb[15].Text = "HFB 16: " + Environment.NewLine + volumes[i].HFB16[5] + " m³";
                    hfb[16].Text = "HFB 17: " + Environment.NewLine + volumes[i].HFB17[5] + " m³";
                    hfb[17].Text = "HFB 18: " + Environment.NewLine + volumes[i].HFB18[5] + " m³";
                    hfb[18].Text = "HFB 19: " + Environment.NewLine + volumes[i].HFB19[5] + " m³";
                    hfb[19].Text = "HFB 92: " + Environment.NewLine + volumes[i].HFB92[5] + " m³";
                    hfb[20].Text = "Self Serve: " + Environment.NewLine + volumes[i].SS[5] + " m³";
                    hfb[21].Text = "Full Serve: " + Environment.NewLine + volumes[i].FS[5] + " m³";
                }
            }
        }

        private void editMapLayoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripSplitButton1.Visible = false;
            toolStripButton1.Visible = true;
            toolStripButton3.Visible = true;

            for (int i = 0; i < hfb.Count()-1; i++)
            {
                hfb[i].Visible = false;
            }

            pictureBox1.Cursor = Cursors.Cross;

            gridlines = 1;
            pictureBox1.Invalidate();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (hfb[0].Visible == false)
            {

                mapManagement.saveMap(paintSQ, storeNumber);

                gridlines = 0;
                for (int i = 0; i < hfb.Count() - 1; i++)
                {
                    hfb[i].Visible = true;
                }

                pictureBox1.Cursor = Cursors.Arrow;
                pictureBox1.Invalidate();
            }
            else
            {
                for (int i = 0; i < hfb.Length-1; i++)
                {
                    databaseAccessability.updateLocation(Convert.ToInt32(storeNumber), hfb[i].Left, hfb[i].Top, i);
                    hfb[i].Visible = true;
                }

                allowMove = !allowMove;
            }
            toolStripButton1.Visible = false;
            toolStripSplitButton1.Visible = true;
            toolStripButton3.Visible = false;
        }

        private void lstDates_SelectedIndexChanged(object sender, EventArgs e)
        {
            updateMap(lstDates.SelectedIndex);
            calcWeekInformation();
        }

        private void editButtonPositionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            allowMove = true;
            toolStripSplitButton1.Visible = false;
            toolStripButton1.Visible = true;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        public void setVolumes(List<volumesPerDay> vol)
        {
            volumes = vol;
            //updateMap(lstDates.SelectedIndex);
        }

        private void frmMain_Activated(object sender, EventArgs e)
        {
            updateMap(lstDates.SelectedIndex);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frmRP planner = new frmRP(Convert.ToInt32(storeNumber), currentYear.ToString());
            //frmResourcePlanning planner = new frmResourcePlanning(storeNumber);
            planner.Show();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Image pencil = Properties.Resources.Pencil;
            Image eraser = Properties.Resources.Eraser;

            if (toolStripButton3.Tag.ToString() == "Erase")
            {
                toolStripButton3.Image = pencil;
                toolStripButton3.Tag = toolStripButton3.Text = "Draw";
                draw = 0;
            }
            else
            {
                toolStripButton3.Image = eraser;
                toolStripButton3.Tag = toolStripButton3.Text = "Erase";
                draw = 1;
            }
        }

        private void toolStripSplitButton2_Click(object sender, EventArgs e)
        {
            frmUpdate update = new frmUpdate(storeNumber, this);
            update.Show();
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            if (lstDates.Items.Count <= 7)
            {
                DialogResult result = MessageBox.Show("We Detect You Have Only 1 Or Less Weeks Data Available" + Environment.NewLine + "Would You Like To Update?", "Update Required", MessageBoxButtons.YesNo);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    frmUpdate update = new frmUpdate(storeNumber, this);
                    update.Show();
                }
            }
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            toolStripSplitButton1.ShowDropDown();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            frmUserManagement user = new frmUserManagement(storeNumber);
            user.Show();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            frmPrintSchedule schedule = new frmPrintSchedule(storeNumber, lstDates.Text);
            schedule.Show();
        }
    }
}
